package com.drinks.yiniang.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.drinks.yiniang.entity.event.PayWXEventBus;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信支付成功回调
 * Created by han on 2017/6/26 0026.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    private String pageType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(getIntent() != null) {
            pageType = getIntent().getStringExtra("_wxapi_payresp_extdata");
        }

        api = WXAPIFactory.createWXAPI(this, Constants.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent != null) {
            pageType = getIntent().getStringExtra("_wxapi_payresp_extdata");
        }
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        //LogUtils.e("baseReq" + baseReq.toString());
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            String error;

            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                error = "支付成功";
            } else if (resp.errCode == BaseResp.ErrCode.ERR_COMM) {
                error = "支付失败: ";
            } else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                error = "取消支付";
            } else {
                error = "未知错误";
            }

            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    ToastUtils.showShort(error);
                    EventBus.getDefault().post(new PayWXEventBus(pageType, PayWXEventBus.PAY_STATUS_SUCCESS));
                    break;
                case BaseResp.ErrCode.ERR_COMM:
                    EventBus.getDefault().post(new PayWXEventBus(pageType, PayWXEventBus.PAY_STATUS_FAIL));
                    ToastUtils.showShort(error + resp.errStr);
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    EventBus.getDefault().post(new PayWXEventBus(pageType, PayWXEventBus.PAY_STATUS_FAIL));
                    ToastUtils.showShort(error);
                    break;
                default:
                    EventBus.getDefault().post(new PayWXEventBus(pageType, PayWXEventBus.PAY_STATUS_FAIL));
                    break;
            }
            finish();
        }
    }
}
